package com.bw.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bw.gulimall.product.entity.CategoryEntity;
import com.bw.gulimall.product.service.CategoryService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;



/**
 * 商品三级分类
 *
 * @author gxx
 * @email sunlightcs@gmail.com
 * @date 2021-08-13 19:41:13
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 递归列表
     */
    @RequestMapping("/tylelist")
    public R tylelist(){
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        QueryWrapper<CategoryEntity> catId = categoryEntityQueryWrapper.eq("parent_cid", 0);
        List<CategoryEntity> list = categoryService.list(catId);
        getchiled(list);
        return R.ok().put("page", list);
    }
    private void getchiled(List<CategoryEntity> list){
    list.forEach(categoryEntity -> {
        List<CategoryEntity> getlist = getlist(categoryEntity.getCatId());
        if (getlist.size()>0){
            categoryEntity.setChildren(getlist);
            getchiled(getlist);
        }
    });
    }
    private List<CategoryEntity> getlist(Long pid){
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        QueryWrapper<CategoryEntity> cat_id = categoryEntityQueryWrapper.eq("parent_cid", pid);
        List<CategoryEntity> list = categoryService.list(cat_id);
        return list;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }


    @RequestMapping("addtree")
    public R addtree(@RequestBody CategoryEntity categoryEntity) {
        Long catId = categoryEntity.getCatId();
        if (catId!=null){
            categoryEntity.setName(categoryEntity.getName2());
            categoryEntity.setName2("");
            categoryService.updateById(categoryEntity);
            return R.ok().put("data",1);
        }
        categoryService.addtree(categoryEntity);
        return R.ok().put("data",2);
    }
}
